package com.yqb.study.modular.controller.data;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.yqb.study.common.controller.BaseController;
import com.yqb.study.common.session.IRedisSession;
import com.yqb.study.common.webscoket.WebSocket;
import com.yqb.study.constant.CodeMsgEnum;
import com.yqb.study.util.FastJsonUtil;
import com.yqb.study.util.ResultVoUtil;

import lombok.extern.slf4j.Slf4j;

// @RestController方法统一返回json报文
@RestController
@Slf4j
@RequestMapping("/data")
public class WebSocketController extends BaseController {
	
	@Autowired
	private WebSocket webSocket;
	
	// 返回一个建立websocket长连接的页面所需数据
	@RequestMapping("/webSocketData")
	public Object webSocketData(IRedisSession sessionRedis) {
		
		Map<String, Object> data = new HashMap<>();
		data.put("phone", sessionRedis.getSessionRedis().getUser().getPhone());
		data.put("token", sessionRedis.getSessionRedis().getToken());
		
		return ResultVoUtil.success(CodeMsgEnum.SERVER_SUCCESS, data);
	}	

    // 测试Redis Session 
	@GetMapping("/helloWebSocket")
	public Object helloWebSocket(IRedisSession sessionRedis) {
		
		// 发送websocket消息        
		String phone = sessionRedis.getSessionRedis().getUser().getPhone();
		String token = sessionRedis.getSessionRedis().getToken();
		webSocket.sendMessage(phone, token, FastJsonUtil.toJson(sessionRedis));
		
		return ResultVoUtil.success(CodeMsgEnum.WEB_SOCKET_SUCCESS);
	}
}
